package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysRoleMenuDao;
import ysq.xyg.sys.model.po.SysRoleMenu;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysRoleMenuVo;
import ysq.xyg.sys.service.SysRoleMenuService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 系统管理-角色菜单service实现
 *
 * @author shengQiang yu
 * @date 2019-4-8 8:55:21
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenu> implements SysRoleMenuService {

    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    private SysRoleMenu sysRoleMenu;

    @Autowired
    public void setBaseDao(SysRoleMenuDao sysRoleMenuDao) {
        this.baseDao = sysRoleMenuDao;
    }


    /**
     * 分页查询
     *
     * @param sysRoleMenuVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    public ReturnEntity page(SysRoleMenuVo sysRoleMenuVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SysRoleMenu> list = acceptList(sysRoleMenuVo);
        PageInfo<SysRoleMenu> page=new PageInfo<SysRoleMenu>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 左关联分页查询
     *
     * @param sysRoleMenuVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    public ReturnEntity pageLeft(SysRoleMenuVo sysRoleMenuVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SysRoleMenu> list = listLeft(sysRoleMenuVo);
        PageInfo<SysRoleMenu> page=new PageInfo<SysRoleMenu>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 左关联查询集合
     *
     * @param sysRoleMenuVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    public List<SysRoleMenu> listLeft(SysRoleMenuVo sysRoleMenuVo) {
        return sysRoleMenuDao.listLeft(sysRoleMenuVo);
    }
    /**
     * 修改
     *
     * @param sysRoleMenuVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    public ReturnEntity addOrUpdate(SysRoleMenuVo sysRoleMenuVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysRoleMenuVo.getId())){
            sysRoleMenu=new SysRoleMenu(currentUser);
            isUpate=false;
        }else{
            sysRoleMenu=selectByPrimaryKey(sysRoleMenuVo.getId());
            if(ObjectUtil.isNull(sysRoleMenu)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysRoleMenu.setUpdateTime(new Date());
            sysRoleMenu.setUpdateUser(currentUser.getId());
        }
        sysRoleMenu.setRoleId(sysRoleMenuVo.getRoleId());
        sysRoleMenu.setMenuId(sysRoleMenuVo.getMenuId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysRoleMenu);
        }else{
            num=insert(sysRoleMenu);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysRoleMenu sysRoleMenu=selectByPrimaryKey(id);
        if(sysRoleMenu!=null){
            sysRoleMenu.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            sysRoleMenu.setUpdateTime(new Date());
            sysRoleMenu.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(sysRoleMenu);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param sysRoleMenuVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-8 8:55:21
     */
    public List<SysRoleMenu> acceptList(SysRoleMenuVo sysRoleMenuVo){
        Example example = new Example(SysRoleMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysRoleMenuVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysRoleMenuVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(sysRoleMenuVo.getRoleId())){
            criteria.andEqualTo("roleId",sysRoleMenuVo.getRoleId());
        }
        if(StringUtil.isNotEmpty(sysRoleMenuVo.getMenuId())){
            criteria.andEqualTo("menuId",sysRoleMenuVo.getMenuId());
        }
        example.setOrderByClause(" update_time desc");
        return sysRoleMenuDao.selectByExample(example);
    }
}
